﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoUbigeo : Form
    {
        [Dependency]
        public IUbigeoBL ubigeoBL { get; set; }

        public FrmMttoUbigeo()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isNew;

        public void NuevoUbigeo()
        {
            isNew = true;
            uBIGEOBindingSource.AddNew();
            readOnly();
            //this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        }

        public void NuevoUbigeoTrabajador(string codigo)
        {
            isNew = true;
            uBIGEOBindingSource.AddNew();

            var ubigeo = uBIGEOBindingSource.Current as UBIGEO;
            ubigeo.Codigo = codigo;
        }

        public UBIGEO _ubigeo { get; set; }

        public void EditarUbigeo()
        {
            isNew = false;
            uBIGEOBindingSource.DataSource = _ubigeo;
        }

        public void readOnly()
        {
            departamentoTextBox.ReadOnly = true;
            provinciaTextBox.ReadOnly = true;
            distritoTextBox.ReadOnly = true;
        }

        private void FrmMttoUbigeo_Load(object sender, EventArgs e)
        {

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var ubigeo = uBIGEOBindingSource.Current as UBIGEO;

            var msg = "ERROR";

            if (ubigeo != null)
            {
                if (isNew == true)
                {
                    try
                    {
                        ubigeo.Departamento = departamentoTextBox.Text.ToUpper();
                        ubigeo.Provincia = provinciaTextBox.Text.ToUpper();
                        ubigeo.Distrito = distritoTextBox.Text.ToUpper();
                        ubigeoBL.AddUbigeo(ubigeo);
                        MessageBox.Show("Registro Correcto", "Registro",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if (isNew == false)
                {
                    try
                    {
                        ubigeo.Departamento = departamentoTextBox.Text.ToUpper();
                        ubigeo.Provincia = provinciaTextBox.Text.ToUpper();
                        ubigeo.Distrito = distritoTextBox.Text.ToUpper();
                        ubigeoBL.UpdateUbigeo(ubigeo);

                        MessageBox.Show("Modificación Correcta", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void btnVerificar_Click(object sender, EventArgs e)
        {
            if (isNew == true)
            {
                if ((codigoTextBox.Text).Length == 6)
                {
                    var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                    var ubi = uBIGEOBindingSource.Current as UBIGEO;
                    if (ubigeo != null)
                    {

                        //ubi.Codigo = ubigeo.Codigo;
                        //ubi.IdUbigeo = ubigeo.IdUbigeo;
                        //ubi.Departamento = ubigeo.Departamento;
                        //ubi.Distrito = ubigeo.Distrito;
                        //ubi.Provincia = ubigeo.Provincia;
                        //uBIGEOBindingSource.ResetBindings(true);

                        MessageBox.Show("Ubigeo no Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //Close();
                    }
                    else
                    {
                        MessageBox.Show("Ubigeo Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);

                        departamentoTextBox.ReadOnly = false;
                        provinciaTextBox.ReadOnly = false;
                        distritoTextBox.ReadOnly = false;
                    }
                }
                else
                {
                    MessageBox.Show("Ubigeo necesita 6 dígitos", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            if (isNew == false)
            {
                if ((codigoTextBox.Text).Length == 6)
                {
                    var ubi = uBIGEOBindingSource.Current as UBIGEO; 
                    var ubigeo = ubigeoBL.ExisteDuplicidad(departamentoTextBox.Text,provinciaTextBox.Text, distritoTextBox.Text,codigoTextBox.Text, ubi.IdUbigeo);
                    
                    if (ubigeo)
                    {
                        //ubi.Codigo = ubigeo.Codigo;
                        //ubi.IdUbigeo = ubigeo.IdUbigeo;
                        //ubi.Departamento = ubigeo.Departamento;
                        //ubi.Distrito = ubigeo.Distrito;
                        //ubi.Provincia = ubigeo.Provincia;
                        //uBIGEOBindingSource.ResetBindings(true);

                        MessageBox.Show("Ya existe Ubigeo con la misma información", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //Close();
                    }
                    else
                    {
                        MessageBox.Show("Ubigeo Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);

                        departamentoTextBox.ReadOnly = false;
                        provinciaTextBox.ReadOnly = false;
                        distritoTextBox.ReadOnly = false;
                    }
                }
                else
                {
                    MessageBox.Show("Ubigeo necesita 6 dígitos", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void codigoTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isNew == true)
                {
                    if ((codigoTextBox.Text).Length == 6)
                    {
                        var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                        var ubi = uBIGEOBindingSource.Current as UBIGEO;
                        if (ubigeo != null)
                        {

                            ubi.Codigo = ubigeo.Codigo;
                            ubi.IdUbigeo = ubigeo.IdUbigeo;
                            ubi.Departamento = ubigeo.Departamento;
                            ubi.Distrito = ubigeo.Distrito;
                            ubi.Provincia = ubigeo.Provincia;
                            uBIGEOBindingSource.ResetBindings(true);

                            MessageBox.Show("Ubigeo no Disponible", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        else
                        {
                            MessageBox.Show("Ubigeo Disponible", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);

                            departamentoTextBox.ReadOnly = false;
                            provinciaTextBox.ReadOnly = false;
                            distritoTextBox.ReadOnly = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ubigeo necesita 6 dígitos", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                if (isNew == false)
                {
                    if ((codigoTextBox.Text).Length == 6)
                    {
                        var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                        var ubi = uBIGEOBindingSource.Current as UBIGEO;
                        if (ubigeo != null)
                        {

                            ubi.Codigo = ubigeo.Codigo;
                            ubi.IdUbigeo = ubigeo.IdUbigeo;
                            ubi.Departamento = ubigeo.Departamento;
                            ubi.Distrito = ubigeo.Distrito;
                            ubi.Provincia = ubigeo.Provincia;
                            uBIGEOBindingSource.ResetBindings(true);

                            MessageBox.Show("Ubigeo no Disponible", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        else
                        {
                            MessageBox.Show("Ubigeo Disponible", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);

                            departamentoTextBox.ReadOnly = false;
                            provinciaTextBox.ReadOnly = false;
                            distritoTextBox.ReadOnly = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ubigeo necesita 6 dígitos", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void codigoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void codigoTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                if (isNew == true)
                {
                    if ((codigoTextBox.Text).Length == 6)
                    {
                        var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                        var ubi = uBIGEOBindingSource.Current as UBIGEO;
                        if (ubigeo != null)
                        {

                            ubi.Codigo = ubigeo.Codigo;
                            ubi.IdUbigeo = ubigeo.IdUbigeo;
                            ubi.Departamento = ubigeo.Departamento;
                            ubi.Distrito = ubigeo.Distrito;
                            ubi.Provincia = ubigeo.Provincia;
                            uBIGEOBindingSource.ResetBindings(true);
                        }
                        else
                        {
                            departamentoTextBox.ReadOnly = false;
                            provinciaTextBox.ReadOnly = false;
                            distritoTextBox.ReadOnly = false;
                        }
                    }

                }

                if (isNew == false)
                {
                    if ((codigoTextBox.Text).Length == 6)
                    {
                        var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                        var ubi = uBIGEOBindingSource.Current as UBIGEO;
                        if (ubigeo != null)
                        {

                            ubi.Codigo = ubigeo.Codigo;
                            ubi.IdUbigeo = ubigeo.IdUbigeo;
                            ubi.Departamento = ubigeo.Departamento;
                            ubi.Distrito = ubigeo.Distrito;
                            ubi.Provincia = ubigeo.Provincia;
                            uBIGEOBindingSource.ResetBindings(true);


                        }
                        else
                        {
                            departamentoTextBox.ReadOnly = false;
                            provinciaTextBox.ReadOnly = false;
                            distritoTextBox.ReadOnly = false;
                        }
                    }
                }
            }
        }

        private void codigoTextBox_Leave(object sender, EventArgs e)
        {
            if (isNew == true)
            {
                if ((codigoTextBox.Text).Length == 6)
                {
                    var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                    var ubi = uBIGEOBindingSource.Current as UBIGEO;
                    if (ubigeo != null)
                    {

                        ubi.Codigo = ubigeo.Codigo;
                        ubi.IdUbigeo = ubigeo.IdUbigeo;
                        ubi.Departamento = ubigeo.Departamento;
                        ubi.Distrito = ubigeo.Distrito;
                        ubi.Provincia = ubigeo.Provincia;
                        uBIGEOBindingSource.ResetBindings(true);

                        MessageBox.Show("Ubigeo no Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Ubigeo Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);

                        departamentoTextBox.ReadOnly = false;
                        provinciaTextBox.ReadOnly = false;
                        distritoTextBox.ReadOnly = false;
                    }
                }
                else
                {
                    MessageBox.Show("Ubigeo necesita 6 dígitos", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            if (isNew == false)
            {
                if ((codigoTextBox.Text).Length == 6)
                {
                    var ubigeo = ubigeoBL.GetFromUbigeoByCodigo(codigoTextBox.Text);
                    var ubi = uBIGEOBindingSource.Current as UBIGEO;
                    if (ubigeo != null)
                    {

                        ubi.Codigo = ubigeo.Codigo;
                        ubi.IdUbigeo = ubigeo.IdUbigeo;
                        ubi.Departamento = ubigeo.Departamento;
                        ubi.Distrito = ubigeo.Distrito;
                        ubi.Provincia = ubigeo.Provincia;
                        uBIGEOBindingSource.ResetBindings(true);

                        MessageBox.Show("Ubigeo no Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Ubigeo Disponible", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);

                        departamentoTextBox.ReadOnly = false;
                        provinciaTextBox.ReadOnly = false;
                        distritoTextBox.ReadOnly = false;
                    }
                }
                else
                {
                    MessageBox.Show("Ubigeo necesita 6 dígitos", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void departamentoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void distritoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void provinciaTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
